package com.zhuiyun.project.api.imagesorting.controller;

import com.zhuiyun.project.api.imagesorting.entity.ImageSorting;
import com.zhuiyun.project.api.imagesorting.service.ImageSortingService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.List;

/**
 * @ClassName ImageSortingController
 * @Description 图片排序controller层
 * @Author zsc
 * @Date 2024/11/11 19:11
 */
@RestController
@RequestMapping("/imageSorting")
@CrossOrigin
public class ImageSortingController extends BaseController{
    @Autowired
    private ImageSortingService imageSortingService;

    /**
    * @Author zsc
    * @Description 新增图片排序
    * @Date 2024/11/11 19:23
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param imageSorting
    */
    @PostMapping("/saveImageSorting")
    public CommonResult saveImageSorting(MultipartRequest multipartRequest,  ImageSorting imageSorting) throws CommonException {
        MultipartFile file = multipartRequest.getFile("file");
        if (imageSorting != null){
           int i = imageSortingService.saveImageSorting(imageSorting,file);
           return CommonResult.ok( i);
        }
        return CommonResult.ok();
    }

    /**
    * @Author zsc
    * @Description 删除图片排序
    * @Date 2024/11/11 19:37
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param
    */
    @DeleteMapping("/deleteImageSorting")
    public CommonResult deleteImageSorting(@RequestParam("id") int id, @RequestParam("imgId")int imgId) throws CommonException {
        if (id > 0){
            int i = imageSortingService.deleteImageSorting(id,imgId);
            return CommonResult.ok(i);
        }
        return CommonResult.ok();
    }

    /**
    * @Author zsc
    * @Description 修改图片排序
    * @Date 2024/11/11 19:43
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param imageSorting
    */
    @PutMapping("/updateImageSorting")
    public CommonResult updateImageSorting(MultipartRequest multipartRequest, ImageSorting imageSorting) throws CommonException {
        MultipartFile file = multipartRequest.getFile("file");
        if (imageSorting != null){
            int i = imageSortingService.updateImageSorting(imageSorting,file);
            return CommonResult.ok(i);
        }
        return CommonResult.ok();
    }

    /**
    * @Author zsc
    * @Description 查询图片排序
    * @Date 2024/11/11 19:52
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @GetMapping("/getImageSortingList")
    public CommonResult getImageSortingList(){
        List<ImageSorting> lists= imageSortingService.getImageSortingList();
        return CommonResult.ok(lists);
    }
}
